package com.xiaoc.bulldozer.controller;

import com.xc.framework.common.page.PageDto;
import com.xc.framework.common.utils.BeanUtil;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.*;
import com.xiaoc.bulldozer.common.model.AwardTimeRule;
import com.xiaoc.bulldozer.common.service.AwardTimeRuleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/awardTimeRule")
public class AwardTimeRuleController extends AdminBaseController {


    @Resource(name = "awardTimeRuleService")
    private AwardTimeRuleService awardTimeRuleService;

    /**
     * 查询列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(AwardTimeRuleQueryDto queryDto) {

        if (queryDto.getPage() == null) {
            queryDto.setPage(1);
        }
        Map<String, Object> params = new HashMap<String, Object>();

        int count = awardTimeRuleService.getAwardTimeRuleCountByCondition(queryDto);

        if (count > 0) {
            List<AwardTimeRule> awardTimeRuleList = awardTimeRuleService.getAwardTimeRuleListByCondition(queryDto);
            params.put("awardTimeRuleList", awardTimeRuleList);
        }

        params.put("queryDto", queryDto);
        params.put("pageDto", new PageDto(queryDto.getPage(), count, PageDto.PAGE_SIZE));
        return renderHtml("awardTimeRule/list.html", params);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ModelAndView detail(Integer id) {

        Map<String, Object> params = new HashMap<String, Object>();

        AwardTimeRule awardTimeRule = null;
        if (id != null) {
            awardTimeRule = awardTimeRuleService.getAwardTimeRuleById(id);
        }

        params.put("awardTimeRule", awardTimeRule);
        return renderHtml("awardTimeRule/edit.html", params);
    }

    @RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST)
    public ModelAndView save(@RequestBody AwardTimeRuleDto awardTimeRuleDto) {


        if (awardTimeRuleDto.getWeight() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写权重");
        }

        AwardTimeRule awardTimeRule = BeanUtil.copyProperties(AwardTimeRule.class, awardTimeRuleDto);


        if (awardTimeRuleDto.getId() != null) {

            awardTimeRule = awardTimeRuleService.getAwardTimeRuleById(awardTimeRuleDto.getId());

            if (awardTimeRule == null) {
                return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "红包时间规则不存在");
            }

            awardTimeRule = BeanUtil.copyProperties(AwardTimeRule.class, awardTimeRuleDto);
            awardTimeRuleService.update(awardTimeRule);

        }else {
            awardTimeRuleService.save(awardTimeRule);
        }

        return jsonSucess();
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ModelAndView delete(@RequestBody AwardTimeRuleDto awardTimeRuleDto) {
        if (awardTimeRuleDto.getId() == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "红包时间规则不存在");
        }
        awardTimeRuleService.deleteById(awardTimeRuleDto.getId());
        return jsonSucess();
    }
}
